package cn.innosmart.aq.manager;

import com.tutk.p2p.ConnectionEntity;
import com.tutk.p2p.ResponseHandler;

import org.json.JSONObject;

/**
 * Created by David on 2015/8/11.
 */
public class LoginManager extends BaseManager {
    private static PingCallBack pingCallBackCallBack;
    private static LoginCallBack loginCallBackCallBack;
    private static ConnectCallBack connectCallBackCallBack;
    private static EncryTopicCheckCallBack encryTopicCheckCallBack;
    private String Tag = "LoginManager";
    private ResponseHandler.ResponseCallback mLoginManagerPingCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "LoginManager.Ping get response=" + message);
            JSONObject jRoot = null;
            int ret = -1;
            try {
                jRoot = new JSONObject(message);
                int rspCode = jRoot.getInt("code");
                String rspMsg = jRoot.getString("message");
                System.out.println("message=" + message);
                if (0 == rspCode && "Success".equals(rspMsg)) {
                }
                pingCallBackCallBack.onPingResponseCallBack(rspCode, rspMsg);
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }
    };
    /**
     * 登陆回调函数
     */
    private ResponseHandler.ResponseCallback mLoginManagerLoginCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "LoginManager.Connect get response=" + message);
            JSONObject jRoot = null;
            int ret = -1;
            try {
                jRoot = new JSONObject(message);
                int rspCode = jRoot.getInt("code");
                String rspMsg = jRoot.getString("message");
                if (0 == rspCode && "Success".equals(rspMsg)) {
                    if (loginCallBackCallBack != null) {
                        loginCallBackCallBack.onResponseCallBack(0, null);
                    }
                } else if (-100 == rspCode) {
                    if (loginCallBackCallBack != null) {
                        loginCallBackCallBack.onResponseCallBack(-1, null);
                    }
                }
            } catch (Exception e) {
                System.out.println(e.toString());
                if (loginCallBackCallBack != null) {
                    loginCallBackCallBack.onResponseCallBack(-2, null);
                }
            }
        }
    };
    //连接回调函数
    private ResponseHandler.ResponseCallback mLoginManagerConnectCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "LoginManager.Connect get response=" + message);
            JSONObject jRoot = null;
            int ret = -1;
            try {
                jRoot = new JSONObject(message);
                int rspCode = jRoot.getInt("code");
                String rspMsg = jRoot.getString("message");
                if (0 == rspCode && "Success".equals(rspMsg)) {
                    String encode_key = jRoot.getString("encode_key");
                    if (connectCallBackCallBack != null) {
                        connectCallBackCallBack.onResponseCallBack(0, encode_key);
                    }
                }
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }
    };
    //Topic验证回调函数
    private ResponseHandler.ResponseCallback mLoginManagerEncryTopicCheckCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "LoginManager.EncryTopicCheck get response=" + message);
            JSONObject jRoot = null;
            int ret = -1;
            try {
                System.out.println("message=" + message);
                jRoot = new JSONObject(message);
                int rspCode = jRoot.getInt("code");
                String rspMsg = jRoot.getString("message");
                if (0 == rspCode && "Success".equals(rspMsg)) {
                    String result = jRoot.getString("r");
//                    if (connectCallBackCallBack != null) {
//                        connectCallBackCallBack.onResponseCallBack(0, encode_key);
//                    }
                    if (encryTopicCheckCallBack != null) {
                        encryTopicCheckCallBack.onResponseCallBack(0, result);
                    }
                }
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }
    };

    private LoginManager() {

    }

    public static LoginManager getInstance() {
        return LoginManagerInstance.instance;
    }

    /**
     * Connection.Ping
     * PingGatwway 的callid 默认为123456789 防止和其他uid重复
     */
    public boolean Ping(ConnectionEntity connectionEntity, String params, PingCallBack pingCallBackCallBack) {
        log(Tag, "Connect. UID = " + connectionEntity.getUid() + ", params = " + params);
        this.pingCallBackCallBack = pingCallBackCallBack;
        long callid = 123456789;
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mLoginManagerPingCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "Connection.Ping", params, callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    /**
     * 新版的连接需要验证密码是否正确
     *
     * @param connectionEntity
     * @param params
     * @param encryTopicCheckCallBack
     * @return
     */
    public boolean EncryTopicCheck(ConnectionEntity connectionEntity, String params, String topic, EncryTopicCheckCallBack encryTopicCheckCallBack) {
        log(Tag, "Connect. UID = " + connectionEntity.getUid() + ", params = " + params);
        this.encryTopicCheckCallBack = encryTopicCheckCallBack;
        long callid = System.currentTimeMillis() / 1000;
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mLoginManagerEncryTopicCheckCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "Connection.EncryptTopicCheck", params, callid, topic)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    /**
     * @param connectionEntity
     * @param params
     * @param connectCallBack
     * @return
     * @deprecated 旧版连接需要先Connect 在Login
     */
    public boolean Connect(ConnectionEntity connectionEntity, String params, ConnectCallBack
            connectCallBack) {
        log(Tag, "Connect. UID = " + connectionEntity.getUid() + ", params = " + params);
        this.connectCallBackCallBack = connectCallBack;
        long callid = System.currentTimeMillis() / 1000;
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mLoginManagerConnectCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        System.out.println("conneting1");
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "Connection.Connect", params, callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    /**
     * username password  clientid
     *
     * @param connectionEntity
     * @param params
     * @param loginCallBackCallBack
     * @return
     */
    // LoginManager.Login
    public boolean Login(ConnectionEntity connectionEntity, String params, LoginCallBack loginCallBackCallBack) {
        log(Tag, "Connect. UID = " + connectionEntity.getUid() + ", params = " + params);
        this.loginCallBackCallBack = loginCallBackCallBack;
        long callid = System.currentTimeMillis() / 1000 + 1;
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mLoginManagerLoginCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "Connection.Login", params, callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    public interface EncryTopicCheckCallBack {
        void onResponseCallBack(int ret, String str);
    }

    public interface ConnectCallBack {
        void onResponseCallBack(int ret, String str);
    }

    public interface PingCallBack {
        void onPingResponseCallBack(int ret, String str);
    }

    public interface LoginCallBack {
        void onResponseCallBack(int ret, String str);
    }

    private static class LoginManagerInstance {
        private static final LoginManager instance = new LoginManager();
    }
}
